package cn.itcast.bos.service.base.impl;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreadRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {
    //注入dao
    @Autowired
    private FixedAreadRepository fixedAreadRepository;
    @Autowired
    private TakeTimeRepository takeTimeRepository;
    @Autowired
    private CourierRepository courierRepository;

    //分页查询
    @Override
    public Page<FixedArea> finAll(Specification<FixedArea> spc, Pageable pageable) {
        return fixedAreadRepository.findAll(spc,pageable);
    }
    //保存区域
    @Override
    public void save(FixedArea entity) {
        fixedAreadRepository.save(entity);
    }


    //关联快递员到定区
    @Override
    public void associateCourier(String fixedAreaId, Integer takeTimeiId,Integer courierId) {
    //持久态的fixarea对象
        FixedArea fixedArea = fixedAreadRepository.findOne(fixedAreaId);
        TakeTime takeTime = takeTimeRepository.findOne(takeTimeiId);
        Courier courier = courierRepository.findOne(courierId);

       courier.setTakeTime(takeTime);
        System.out.println(fixedAreaId);
       fixedArea.getCouriers().add(courier);
    }


    /*@Override
    public Page<FixedArea> findPageData(Specification<FixedArea> specification, Pageable pageable) {
        //调用dao层
        return fixedAreadRepository.findAll(specification, pageable);
    }*/
}
